package com.landscope.issue.service.applet;

import cn.hutool.core.date.DateUtil;
import com.landscope.issue.controller.base.ResponseResult;
import com.landscope.issue.controller.wx.dto.AppletMonitorDataDto;
import com.landscope.issue.controller.wx.dto.AppletStationListDto;
import com.landscope.issue.controller.wx.dto.AppletStationViewDto;
import com.landscope.issue.controller.wx.param.*;
import com.landscope.issue.entity.TopParam;
import com.landscope.issue.entity.User;
import com.landscope.issue.mapper.StainfoMapper;
import com.landscope.issue.mapper.TopParamMapper;
import com.landscope.issue.utils.TokenUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ApiAppletStationService {

    @Resource
    private StainfoMapper stainfoMapper;

    public ResponseResult<AppletStationListDto> list(AppletStationListParam param) {
        TokenUtil.verifyAuthority(param.getAccessToken(), param.getProjectCode());
        AppletStationListDto dto = new AppletStationListDto();
        List<AppletStationListDto.AppletStationListItem> list = stainfoMapper.findAppletDataListByCode(param.getProjectCode());

        for (AppletStationListDto.AppletStationListItem item :
                list) {
            if (item.getStatus() == 1)
                item.setLevel(1);
            else if (item.getStatus() == 2)
                item.setLevel(3);
        }

        dto.setDate(DateUtil.format(new Date(), "MM月dd日"));
        // 获取设备异常数量
        Integer exceptionNum = stainfoMapper.findExceptionNumByCode(param.getProjectCode());
        dto.setExceptionNum(exceptionNum);
        dto.setStaList(list);
        return new ResponseResult<AppletStationListDto>().success().setData(dto);
    }

    public ResponseResult<AppletStationViewDto> view(AppletStationViewParam param) {
        TokenUtil.verifyAuthority(param.getAccessToken(), param.getProjectCode());
        AppletStationViewDto dto = stainfoMapper.findAppletDataListById(param.getId());
        if (dto.getStatus() == 1)
            dto.setLevel(1);
        else if (dto.getStatus() == 2)
            dto.setLevel(3);
        return new ResponseResult<AppletStationViewDto>().success().setData(dto);
    }
}
